package com.pbd.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MemberFullView extends Composite {

	private Long partyId;
	private GoogleMap googlePopup;
	private DeckPanel deckPanel;
	private VerticalPanel vpAlbum;

	public MemberFullView() {
		initUI();
	}

	public MemberFullView(Long partyId) {
		this.partyId = partyId;
		initUI();
	}

	private void initUI() {
		deckPanel = new DeckPanel();
		FlexTable mainTable = new FlexTable();
		mainTable.setWidth("660px");
		mainTable.setStyleName("left_blue_box1");

		final FlexTable innerTable = new FlexTable();
		innerTable.setWidth("640px");
		innerTable.setStyleName("white_box");
		initWidget(deckPanel);
		deckPanel.add(mainTable);
		vpAlbum = new VerticalPanel();
		deckPanel.add(vpAlbum);
		mainTable.setWidget(0, 0, innerTable);

		ServiceUtil.getMemberServiceAsync().getPartyInfoMap(partyId, new AsyncCallback<Map>() {

			@Override
			public void onSuccess(Map memberMap) {

				FlexTable partyTopTable = new FlexTable();
				partyTopTable.setStyleName("blue_box_hd");
				String name = (String) memberMap.get("Name");
				final String address = (String) memberMap.get("Address");
				String phone = (String) memberMap.get("Phone");
				String fax = (String) memberMap.get("Fax");
				String mobile = (String) memberMap.get("Mobile");
				String email = (String) memberMap.get("Email");
				String webSite = (String) memberMap.get("WebSite");
				String location = (String) memberMap.get("Area");
				String postedOn = (String) memberMap.get("PostedOn");
				String partyID = (String) memberMap.get("Id");
				String logoPath = (String) memberMap.get("LogoPath");
				List albumList = (List) memberMap.get("AlbumList");

				partyTopTable.setWidget(0, 0, new Image(".\\images\\arrow_02.png"));
				partyTopTable.setWidget(0, 1, new HTML(name));
				FlexTable editPartyLink = new FlexTable();

				Anchor editProfileAnchor = new Anchor("Edit Profile");
				editProfileAnchor.setStyleName("hyperLnk2");
				editPartyLink.setWidget(0, 0, new Image(".\\images\\edit_icon.png"));
				editPartyLink.setWidget(0, 1, editProfileAnchor);
				editProfileAnchor.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						FlexTable mainTable = (FlexTable) RootPanel.get("mainContainer").getWidget(0);
						mainTable.clear();
						mainTable.setCellSpacing(8);
						mainTable.setWidget(0, 0, new MemberRegistrationScreen(partyId));

						FlexTable adsTable = new FlexTable();
						adsTable.setStyleName("gray_box");
						adsTable.setWidth("250px");
						adsTable.setHeight("757px");
						adsTable.setWidget(0, 0, new HTML("<p><span class=\"body_txt_black\"></span><span class=\"heading_blue_s2\"><br></span><span class=\"heading_blue\">ADVERTISE<br>YOUR<br>BUSINESS</span><span class=\"body_txt_black1\"><br><br></span><span class=\"heading_orange\"><font color=\"#00e1ff\" size=\"+4\"><b>HERE</b></font></span><br><br></p><p class=\"heading_orange1\"><span class=\"body_txt_black\">Please Contact:</span><br><img src=\".\\images\\phone_icon.png\"/>&nbsp&nbsp<span class=\"heading_orange1\">6529 1930 / 2235 0986</span></p>"));
						mainTable.setWidget(0, 1, adsTable);
					}
				});
				// editPartyLink.getFlexCellFormatter().setStyleName(0, 1,
				// "hyperLnk2");
				editPartyLink.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
				editPartyLink.getFlexCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);
				editPartyLink.setStyleName("heading_white1");
				partyTopTable.setWidget(0, 2, editPartyLink);
				partyTopTable.getFlexCellFormatter().setStyleName(0, 1, "heading_white1");
				// partyTopTable.getFlexCellFormatter().setStyleName(0, 2,
				// "heading_white1");
				partyTopTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_RIGHT);
				editPartyLink.setVisible(false);
				partyTopTable.getFlexCellFormatter().setWidth(0, 0, "40px");
				partyTopTable.getFlexCellFormatter().setWidth(0, 1, "520px");
				partyTopTable.getFlexCellFormatter().setWidth(0, 2, "100px");
				partyTopTable.getFlexCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_MIDDLE);
				FlexTable centerTable = new FlexTable();
				if (logoPath == null)
					centerTable.setWidget(0, 0, new Image(".\\images\\photo_un.gif"));
				else {
					Image logoImage = new Image("." + logoPath);
					logoImage.setWidth("88px");
					logoImage.setHeight("88px");
					centerTable.setWidget(0, 0, logoImage);
				}

				// centerTable.setWidget(0, 0, new
				// Image(".\\images\\photo_un.gif"));
				centerTable.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);

				FlexTable centerContentTable = new FlexTable();
				centerContentTable.setWidget(0, 0, new HTML((String) memberMap.get("Contact Person")));
				centerContentTable.setWidget(1, 0, new Image(".\\images\\company_icon.png"));
				centerContentTable.getFlexCellFormatter().setStyleName(0, 0, "heading_blue_s1");
				centerContentTable.getFlexCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_TOP);

				// partyTopTable.getFlexCellFormatter().setWidth(0, 0, "40px");
				// centerContentTable.getFlexCellFormatter().setWidth(0, 1,
				// "700px");
				// partyTopTable.getFlexCellFormatter().setWidth(0, 2, "100px");

				if (address != null && address.trim().length() > 0) {
					centerContentTable.setWidget(1, 1, new HTML(address + "<br>" + "Pin " + (String) memberMap.get("PinCode")));
					final Anchor viewMap = new Anchor("View Map");
					viewMap.setLayoutData(address);
					StringBuffer sbMap = new StringBuffer("<span>");
					sbMap.append("<img src=\".\\images\\arrow_04.png\" width=\"15\" height=\"7\">");
					sbMap.append("<a class=\"hyperLnk\">View Map</a>");
					sbMap.append("</span");

					centerContentTable.setWidget(1, 2, viewMap);
					centerContentTable.getFlexCellFormatter().setWidth(1, 2, "150px");
					viewMap.addClickHandler(new ClickHandler() {

						@Override
						public void onClick(ClickEvent event) {

							final PopupPanel popupPanel = new PopupPanel();
							popupPanel.setAutoHideEnabled(true);
							popupPanel.setPopupPosition(viewMap.getAbsoluteLeft(), viewMap.getAbsoluteTop());
							googlePopup = new GoogleMap("600px", "300px", ((String) viewMap.getLayoutData()).replace("<br>", ""), null, null, new AsyncCallback() {

								@Override
								public void onFailure(Throwable caught) {
									// TODO Auto-generated method stub

								}

								@Override
								public void onSuccess(Object result) {
									popupPanel.clear();
									popupPanel.add(googlePopup);
									popupPanel.show();
								}
							});

						}
					});
					centerContentTable.getFlexCellFormatter().setStyleName(1, 1, "body_txt_black");
					centerContentTable.getFlexCellFormatter().setVerticalAlignment(1, 2, HasVerticalAlignment.ALIGN_BOTTOM);
				}
				if (phone != null && phone.trim().length() > 0) {
					int rowCount = centerContentTable.getRowCount();
					centerContentTable.setWidget(rowCount, 0, new Image(".\\images\\phone_icon.png"));
					centerContentTable.setWidget(rowCount, 1, new HTML(phone));
					// centerContentTable.getFlexCellFormatter().setStyleName(1,
					// 1, "body_txt_black");

				}
				if (fax != null && fax.trim().length() > 0) {
					int rowCount = centerContentTable.getRowCount();
					centerContentTable.setWidget(rowCount, 0, new Image(".\\images\\fax_icon.png"));
					centerContentTable.setWidget(rowCount, 1, new HTML(fax));
					// sbContent.append("<br> <span class=\"body_txt_blue2\">Fax :</span>");
					// sbContent.append(fax);
				}
				if (mobile != null && mobile.trim().length() > 0) {
					int rowCount = centerContentTable.getRowCount();
					centerContentTable.setWidget(rowCount, 0, new Image(".\\images\\mobile_icon.png"));
					centerContentTable.setWidget(rowCount, 1, new HTML(mobile));
					// sbContent.append("<br> <span class=\"body_txt_blue2\">Mobile :</span>");
					// sbContent.append(mobile);
				}

				if (email != null && email.trim().length() > 0) {
					int rowCount = centerContentTable.getRowCount();
					centerContentTable.setWidget(rowCount, 0, new Image(".\\images\\email_icon.png"));
					String[] emailArray = email.split(",");
					StringBuffer sbEmail = new StringBuffer("<span>");

					if (emailArray.length == 0)
						sbEmail.append("<a href=\"mailto:" + email + "\" class=\"hyperLnk1\">" + email + "</a>");
					for (int index = 0; index < emailArray.length; index++) {
						sbEmail.append("<a href=\"mailto:" + emailArray[index] + "\" class=\"hyperLnk1\">" + emailArray[index] + "</a>");
						if (index < emailArray.length - 1)
							sbEmail.append(",");
					}
					sbEmail.append("</span>");
					centerContentTable.setWidget(rowCount, 1, new HTML(sbEmail.toString()));

					// sbContent.append("<br> <span class=\"body_txt_blue2\">Email :</span>");
					// sbContent.append("<a href=\"mailto:"+email+"\" class=\"hyperLnk1\">"+email+"</a>");
				}

				if (webSite != null && webSite.trim().length() > 0) {
					int rowCount = centerContentTable.getRowCount();
					centerContentTable.setWidget(rowCount, 0, new Image(".\\images\\web_icon.png"));
					Anchor webLink = new Anchor(webSite);
					if (webSite.startsWith("http://"))
						webLink.setHref(webSite);
					else
						webLink.setHref("http://" + webSite);

					webLink.setTarget(webSite);

					centerContentTable.setWidget(rowCount, 1, webLink);

					// sbContent.append("<br> <span class=\"body_txt_blue2\">Email :</span>");
					// sbContent.append("<a href=\"mailto:"+email+"\" class=\"hyperLnk1\">"+email+"</a>");
				}

				// sbContent.append("</p>");

				// centerContentTable.setWidget(1, 1, new
				// HTML(sbContent.toString()));
				centerContentTable.getFlexCellFormatter().setColSpan(0, 0, 2);
				centerTable.setWidget(0, 1, centerContentTable);
				centerTable.getFlexCellFormatter().setStyleName(0, 1, "blank_box");

				innerTable.setWidget(0, 0, partyTopTable);
				innerTable.setWidget(1, 0, centerTable);
				// StringBuffer sbDot = new
				// StringBuffer("<td class=\"dot\"> </td>");
				innerTable.setWidget(2, 0, new HTML("<td></td>"));
				innerTable.getFlexCellFormatter().setStyleName(2, 0, "dot");
				if (albumList != null && albumList.size() > 0) {
					FlexTable logoTable = new FlexTable();
					logoTable.setWidget(0, 0, new HTML(""));
					logoTable.setWidget(0, 1, addAlbums(albumList));
					logoTable.getFlexCellFormatter().setWidth(0, 0, "88px");

					innerTable.setWidget(3, 0, logoTable);
					innerTable.setWidget(4, 0, new HTML("<td></td>"));
					innerTable.getFlexCellFormatter().setStyleName(4, 0, "dot");
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		});
		deckPanel.showWidget(0);
	}

	private FlexTable addAlbums(List albumList) {
		FlexTable innerLogo = new FlexTable();
		int captionRowIndex = 0;
		int logoRowIndex = 1;
		int size = albumList == null ? 0 : albumList.size();
		int columnIndex = 0;
		for (int index = 0; index < size; index++) {
			ArrayList dataValueList = (ArrayList) albumList.get(index);
			Image image = new Image("." + dataValueList.get(3));
			image.setPixelSize(172, 125);
			//image.setWidth("152px");
			//image.setWidth("100px");
			ArrayList layOutDataList = new ArrayList();
			layOutDataList.add(dataValueList.get(0));
			layOutDataList.add(dataValueList.get(2));
			layOutDataList.add(dataValueList.get(3));
			image.setLayoutData(layOutDataList);

			if (index > 0 && index % 3 == 0) {
				captionRowIndex = captionRowIndex+ 2;
				logoRowIndex = logoRowIndex+2;
				columnIndex = 0;
			}

			innerLogo.setWidget(captionRowIndex, columnIndex, new HTML((String) dataValueList.get(1)));
			innerLogo.setWidget(logoRowIndex, columnIndex, image);
			addImageClickHandler(image);
			innerLogo.getFlexCellFormatter().setStyleName(captionRowIndex, columnIndex, "heading_blue_s2");
			// innerLogo.getFlexCellFormatter().setStyleName(logoRowIndex,
			// index, "photo_bg");
			innerLogo.getFlexCellFormatter().setWidth(captionRowIndex, columnIndex, "193px");
			// innerLogo.getFlexCellFormatter().setWidth(logoRowIndex, index,
			// "132px");
			innerLogo.getFlexCellFormatter().setHorizontalAlignment(captionRowIndex, columnIndex, HasHorizontalAlignment.ALIGN_CENTER);
			innerLogo.getFlexCellFormatter().setHorizontalAlignment(logoRowIndex, columnIndex, HasHorizontalAlignment.ALIGN_CENTER);
			innerLogo.getFlexCellFormatter().setVerticalAlignment(logoRowIndex, columnIndex, HasVerticalAlignment.ALIGN_MIDDLE);
			columnIndex++;
		}
		return innerLogo;
	}

	private void addImageClickHandler(final Image thumbNailImage) {
		thumbNailImage.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				ArrayList layoutList = (ArrayList) thumbNailImage.getLayoutData();
				PopupPanel popup = new PopupPanel(true);
				// popup.setStyleName("modal");
				String layout = (String) layoutList.get(1);
				if (layout.equalsIgnoreCase("Single Photo")) {
					Image img = new Image("." +(String) layoutList.get(2));
					//img.setPixelSize(250, 250);
					popup.add(img);
					popup.setGlassEnabled(true);
					popup.center();
					popup.show();
				}else{
					vpAlbum.clear();
					vpAlbum.add(new AlbumViewer((Long)layoutList.get(0)));
					HTML html = new HTML("<B> Back to Profile");
					vpAlbum.add(html);
					deckPanel.showWidget(1);
					html.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							vpAlbum.clear();
							deckPanel.showWidget(0);
						}
					});
				}

			}
		});

	}
}
